import path from 'path';
import db from '../../data/db';
const TABLE = path.basename(__dirname);//默认表名称为当前文件夹名称
const model = db.get(TABLE);
const columns = 'Id,Code,UserName,Name,NameJp,TitleCode,Ip,DateStart,DateEnd,Status,AuthData'.split(',');
export function get({pageSize = 0, page = 0} = {}) {
    const ps = model;
    return pageSize && page
        ? ps.fetchPage({pageSize, page, columns})
        .then(result =>
            ps.count().then(
                count => ({
                    count,
                    data: result
                }))
        )
        : ps.fetchAll({columns});
}

export function save(data) {
    return new model(data).save();
}

export function del(id) {
    return model.forge({Id: id}).destroy();
}

export function find(where) {
    return model
        .where(where)
        .fetch()
        .then(user =>
            Promise.resolve(user && user.toJSON())
        );
}
